package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    //  http://localhost/admin/product/1/10?category3Id=61
    //  category3Id=61 有很多种方式！ HttpServletRequest request  @RequestParam Long category3Id
    //  对象传值： 当传入的参数名与实体类的属性名称一致的时候，可以使用实体类接收这个参数！
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo){
        //  mybatis-plus 有分页类！
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        IPage iPage = manageService.getSpuInfoList(spuInfoPage,spuInfo);
        //  返回数据！ List<T> getRecords()
        return Result.ok(iPage);

    }

    // /admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        //  调用服务层方法
        List<BaseSaleAttr>  baseSaleAttrList = this.manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //  http://localhost/admin/product/saveSpuInfo
    //  Json ---> Java Object
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  保存方法！
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  /admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  调用服务层方法
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  http://localhost/admin/product/spuSaleAttrList/9
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //  调用服务层方法
        List<SpuSaleAttr> spuSaleAttrList = this.manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @ApiOperation(value = "获取spu基本信息")
    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId) {
        return Result.ok(manageService.getFullSpuInfo(spuId));
    }

    @ApiOperation(value = "更改spu信息")
    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){
        // 调用服务层的保存方法
        manageService.updateSpuInfo(spuInfo);
        return Result.ok();
    }
}
